package lab03.test07;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Test {
    public static void main(String[] args) {

        /*
        * 读取book.zip,并将book.zip中含有的文件重新存放到当前目录中的book文件夹中
        * 即将book.zip的内容解压到book文件夹中
        * */

        String path1 = "D:\\dev\\lab03\\TestZip.zip";
        String path2 = "D:\\dev\\lab03\\TestZip";



        ZipInputStream zipInputStream = null;
        ZipFile zipFile = null;

        try {

            zipFile = new ZipFile(path1);//创建一个zip文件对象
            Enumeration entries = zipFile.entries();
            ZipEntry zipEtry = null;


            while (entries.hasMoreElements()){
                zipEtry = (ZipEntry) entries.nextElement();
                System.out.println(zipEtry.getName());


                //创建 TestZip 文件夹
                File testFile = new File(path2);
                testFile.mkdir();//创建文件夹



                //字节流 读文件 写文件
                InputStream inputStream = zipFile.getInputStream(zipEtry);
                FileOutputStream outputStream = new FileOutputStream(
                        new File("D:\\dev\\lab03\\TestZip\\"+zipEtry.getName())
                );

                byte[] data = new byte[1024];
                while ((inputStream.read(data))!=-1){
                    outputStream.write(data);
                }

                inputStream.close();
                outputStream.close();



            }




        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
